﻿using Dapr.Client;
using VaccinationHost.Extend;
using VaccinationHost.Model;
using VaccinationHost.Model.Dto;

namespace VaccinationHost.Services
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class UserService: IUserService
    {
        private readonly HttpClient _httpClient;

        public UserService(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        //public string OpenId { get; set; }

        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task<UserDto> CurrentUserAsync(string openId)
        {
            // TODO 服务端待对接

            if (openId == "123456")
            {
                return new UserDto() { Id="999999", UserName="张三", OpenId="123456" };
            }
            else
            {
                return null;
            }

            var getmsg = await _httpClient.GetAsync($"api/v1/userinfo/{openId}/ByOpenId");
            var regresult = await getmsg.ToResult();
            if(regresult ==null|| regresult is OpResult)
            {
                return null;
            }
            else
            {
                return regresult.ToString().ToObject<UserDto>();
            }
        }
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<OpResult> Register(string openId, string name)
        {
            OpResult result = new OpResult();
            if (string.IsNullOrWhiteSpace(openId))
            {
                result.Code = 400;
                result.Message = "未获取到微信信息。";
                return result;
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                result.Code = 400;
                result.Message = "未设置名称。";
                return result;
            }

            //if (openId == "666666")
            //{
            //    result.Code = 200;
            //    result.Message = "注册成功。";
            //    UserDto user = new UserDto() { Id = "", OpenId = "666666", UserName = name };
            //    result.Data = user;
            //    return result;
            //}
            //else
            //{
            //    result.Code = 400;
            //    result.Message = "注册失败。";

            //    return result;
            //}

            var request = new HttpRequestMessage(HttpMethod.Post, "api/v1/userinfo/add")
            {
                Content = JsonContent.Create(new { Name=name, OpenId=openId })
            };

            //request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            var response = await _httpClient.SendAsync(request);

            var regresult = await response.ToResult();
            if(regresult is OpResult)
            {
                return regresult as OpResult;
            }
            else
            {
                result.Code = 200;
                result.Message = "注册成功。";
            }

            return result;
        }
    }
}
